<?php
namespace Home\Controller;
class UserController extends BaseController {
	public function main() {
		if($_POST['info']=='exit'){
				session_unset();
				header("Location:http:/home/index/index");
			}
		$username=$_SESSION['username'];
		$this->assign("user","{$username}");
		$this -> display();
	}
	public function items(){
		if(IS_POST){
			if($_POST['info']=='exit'){
				session_unset();
				header("Location:http:/home/index/index");
			}
			if($_POST['edit']){
				$id=$_POST['id'];
				$m=M('userinfo');
				$ret=$m->where("id='{$id}'")->select();
				$this->ajaxReturn($ret);
			}
			if($_POST['read']){
				$id=$_POST['id'];
				$m=M('userinfo');
				$ret=$m->where("id='{$id}'")->select();
				$this->ajaxReturn($ret);
			}
			if($_POST['del']){
				$id=$_POST['id'];
				$m=M('userinfo');
				$result = $m->where('id='.$id)->delete();
				$this->ajaxReturn($result);
			}
			if($_POST['username']){
				$data['username']=$_POST['username'];
				$data['nickname']=$_POST['nickname'];
				$pass=md5($_POST['newpass']);
				$data['password']=$pass;
				$data['phone']=$_POST['phone'];
			 	$data['birthday']=$_POST['birthday'];
				$data['address']=$_POST['address'];
//				$this->ajaxReturn(md5($_POST['pass']));
				$Form = M('userinfo');
				$res=$Form->where("username='".$_POST['username']."'")->select();
				if($res){
					$obj=(object)[];
						$obj->code=1;
						$obj->message="失败";
						$this->ajaxReturn($obj);
				}else{
					$result = $Form -> add($data);
					$title=$_POST['title'];
					$group=M('auth_group');
					$group_id=$group->field('id')->where("title='{$title}'")->find();
					$access=M('auth_group_access');
					$add['uid']=$result;
					$add['group_id']=$group_id['id'];
					$access->add($add);
					if($result){
						$obj=(object)[];
						$obj->code=0;
						$obj->message="成功";
						$this->ajaxReturn($obj);
					}
				}
			}
			if($_POST['ename']){
				$data['name']=$_POST['ename'];
				$data['phone']=$_POST['ephone'];
				$data['address']=$_POST['eaddress'];
				$data['id']=$_POST['eid'];
				$data['nickname']=$_POST['enick'];
				$data['birthday']=$_POST['ebirthday'];
				
				$Form = M('userinfo');
				$res=$Form->where("id='".$_POST['eid']."'")->select();
//				$this->ajaxReturn($res[0]['nickname']);
				if($res[0]['phone']==$_POST['ephone']&&$res[0]['address']==$_POST['eaddress']&&$res[0]['nickname']==$_POST['enick']&&$res[0]['birthday']==$_POST['ebirthday']){
					$obj=(object)[];
					$obj->code=1;
					$obj->message="失败";
					$this->ajaxReturn($obj);
				}else{
					$result = $Form -> save($data);
						$obj=(object)[];
						$obj->code=0;
						$obj->message="成功";
						$this->ajaxReturn($obj);
				}
			}
		}else{
			$username=$_SESSION['username'];
			$this->assign("user","{$username}");
			$this -> display();
		}
	}
	public function auth(){
		if($_POST['info']=='exit'){
				session_unset();
				header("Location:http:/home/index/index");
			}
		$username=$_SESSION['username'];
		$this->assign("user","{$username}");
		$this -> display();
	}
	public function info(){
		if(IS_POST){
			if($_POST['info']=='exit'){
				session_unset();
				header("Location:http:/home/index/index");
			}
			$data['phone']=$_POST['phone'];
			$data['birthday']=$_POST['birthday'];
			$data['address']=$_POST['address'];
			$data['id']=$_SESSION['id'];
			$Form = D('userinfo');
			$result = $Form -> save($data);
			if ($result) {
				$obj=(object)[];
				$obj->code=0;
				$obj->message="成功";
				$this->ajaxReturn($obj);
			} else {
				$obj=(object)[];
				$obj->code=103;
				$obj->message="失败";
				$this->ajaxReturn($obj);
			}
		}else{
			$username=$_SESSION['username'];
			$m=M('userinfo');
			$ret=$m->where("username='{$username}'")->select();
			$phone=$ret[0]['phone'];
			$birthday=$ret[0]['birthday'];
			$address=$ret[0]['address'];
			$this->assign("user","{$username}");
			$this->assign("phone","{$phone}");
			$this->assign("birthday","{$birthday}");
			$this->assign("address","{$address}");
			$this -> display();
		}
	}
	public function updatePwd(){
		
		if(IS_POST){
			if($_POST['info']=='exit'){
				session_unset();
				header("Location:http:/home/index/index");
			}
			$yspass=$_POST["YsPassword"];
			$newpass=$_POST["NewPassword"];
			$id=$_SESSION['id'];
			$pass=md5($yspass);
			$m=M('userinfo');
			$ret=$m->where("password='{$pass}' and id='{$id}'")->select();
			$count=count($ret);
//			
			if($count==1){
				$data['id']=$id;
				$data['password']=md5($newpass);
				$result = $m -> save($data);
				if($result){
					$obj=(object)[];
					$obj->code=0;
					$obj->message="成功";
					$this->ajaxReturn($obj);
				}
			}else{
				$obj=(object)[];
				$obj->code=103;
				$obj->message="失败";
				$this->ajaxReturn($obj);
			}
		}else{
			$username=$_SESSION['username'];
			$this->assign("user","{$username}");
			$this -> display();
		}
	}
	public function Role(){
		if(IS_POST){
			if($_POST['info']=='exit'){
				session_unset();
				header("Location:http:/home/index/index");
			}
			if($_POST['addtitle']){
				$data['title']=$_POST['addtitle'];
				$data['beizhu']=$_POST['addbeizhu'];
				$data['status']=1;
				$data['rules']="1,2,9";
				$m=M('auth_group');
				$result=$m->where("title=".$_POST['addtitle']."")->select();
				if($result['title']==$_POST['addtitle']){
					$obj=(object)array();
					$obj->code=1;
					$obj->message="添加失败";
					$this->ajaxReturn($obj);
				}else{
					$ret=$m->add($data);
					if($ret){
						$obj=(object)array();
						$obj->code=0;
						$obj->message="添加成功";
						$this->ajaxReturn($obj);
					}
				}
			}
			if($_POST['edittitle']){
				$data['title']=$_POST['edittitle'];
				$data['beizhu']=$_POST['editbeizhu'];
				$data['id']=$_POST['editid'];
				$m=M('auth_group');
				$result=$m->where("id=".$_POST['editid']."")->select();
				if($result['title']==$_POST['edittitle']&&$result['beizhu']==$_POST['editbeizhu']){
					$obj=(object)array();
					$obj->code=1;
					$obj->message="更改失败";
					$this->ajaxReturn($obj);
				}else{
					$ret=$m->save($data);
					if($ret){
						$obj=(object)array();
						$obj->code=0;
						$obj->message="更改成功";
						$this->ajaxReturn($obj);
					}
				}
			}
			//删除角色
			if($_POST['deltitle']){
				$id=$_POST['id'];
				$m=M('auth_group');
				$z=M('auth_group_access');
				$ret=$z->where('group_id='.$id)->select();
				if($ret){
					$obj->code=1;
					$this->ajaxReturn($obj);
				}else{
					$result = $m->where('id='.$id)->delete();
					if($result){
						$obj->code=0;
						$this->ajaxReturn($obj);
					};
				}
//				$this->ajaxReturn($ret);
			}
			//删除角色中用户
			if($_POST['deluser']){
				$username=$_POST['deluser'];
				$m=M('userinfo');
				$uid = $m->field('id')->where("username='{$username}'")->find();
				$z=M('auth_group_access');
				$group_id=$z->field('group_id')->where("uid=".$uid['id'])->find();
				if($group_id['group_id']==1){
					$obj->code=2;
					$this->assign("message","这是超级管理员不能删除");
				}else{
					$result = $z->where('uid='.$uid['id'].' and group_id='.$group_id['group_id'])->delete();
					if($result){
						$obj->code=0;
						$this->ajaxReturn($obj);
					}else{
						$obj->code=1;
						$this->ajaxReturn($obj);
						$this->assign("message","请重新进行操作");
					}
				}
			}
		}else{
			$username=$_SESSION['username'];
			$this->assign("user","{$username}");
			$this -> display();
		}
		
		
	}
	public function listitems(){
		$ret=(object)[];//创建一个对象
		$ret->data=array();
		for($i=1;$i<200;$i++){
			$arr=array($i,"姓名".rand(1, 5000),rand(0, 1)?"男":"女",rand(20, 50),);
			$arr[]='<div class="btn-group"><button type="button" class="btn btn-info">查看</button><button type="button" class="btn btn-info">编辑</button><button type="button" class="btn btn-danger">删除</button></div>';
			$ret->data[]=$arr;
		}
		$this->ajaxReturn($ret);
	}
	public function dblist(){
		$columnindex=$_GET['order'][0]['column'];
		$dir=$_GET['order'][0]['dir'];
		$start=$_GET['start'];
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		$m=M('userinfo');
		if($columnindex==1){
			$column='username';
		}else if($columnindex==2){
			$column='nickname';
		}else if($columnindex==3){
			$column='phone';
		}else{
			$column='id';
		}
		$res=$m->where("username like '%{$search}%' or nickname like '%{$search}%' or phone like '%{$search}%'")->order($column." " .$dir)->limit("{$start},{$length}")->select();
		$total=$m->select();
		$ret=(object)[];
		//创建一个对象
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($res);$i++){
			$ret->data[]=array($i+1,$res[$i]["username"],$res[$i]["nickname"],$res[$i]["phone"],'<div class="btn-group"><button type="button" class="btn btn-info read" data-read="read" data-id="'.$res[$i]["id"].'" data-toggle="modal" data-target="#modal-default">查看</button><button data-edit="edit" data-id="'.$res[$i]["id"].'" type="button" class="btn btn-info edit"  data-toggle="modal" data-target="#modal-default2">编辑</button><button data-del="del"  data-id="'.$res[$i]["id"].'" id="del" type="button" class="btn btn-danger">删除</button></div>');
		}
		$ret->recordsFiltered=count($res);
		$ret->recordsTotal=count($total);
		$this->ajaxReturn($ret);
	}
	public function authlist(){
		$columnindex=$_GET['order'][0]['column'];//获取升降序根据的列的索引
		$dir=$_GET['order'][0]['dir'];//根据什么排序
		$start=$_GET['start'];//表格
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		$m=M('auth_group');
		if($columnindex==1){
			$column='title';
		}else if($columnindex==2){
			$column='id';
		}else if($columnindex==3){
			$column='beizhu';
		}
		$res=$m->where("title like '%{$search}%' or id like '%{$search}%' or beizhu like '%{$search}%'")->order($column." " .$dir)->limit("{$start},{$length}")->select();
		$total=$m->select();
		$ret=(object)[];
		//创建一个对象
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($res);$i++){
			$ret->data[]=array($i+1,$res[$i]["title"],$res[$i]["id"],$res[$i]["beizhu"]);
		}
		$ret->recordsFiltered=count($res);
		$ret->recordsTotal=count($total);
		$this->ajaxReturn($ret);
	}
	public function rulelist(){
		$id=$_POST['id'];
		$rulesid=$this->getRule($id);
		$columnindex=$_GET['order'][0]['column'];//获取升降序根据的列的索引
		$dir=$_GET['order'][0]['dir'];//根据什么排序
		$start=$_GET['start'];//表格
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		$m=M('auth_rule');
		if($columnindex==1){
			$column='title';
		}
//		$res=$m->where("title like '%{$search}%' and id in(".$rulesid.") and is_menu =1 and pid !=0")->order($column." " .$dir)->limit("{$start},{$length}")->select();
		$res=$m->where("title like '%{$search}%' and id in(".$rulesid.") and is_menu =1 and pid !=0")->select();
		$total=$m->where("id in(".$rulesid.") and is_menu =1 and pid !=0")->select();
		$ret=(object)[];
		//创建一个对象
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($res);$i++){
			$ret->data[]=array($i+1,$res[$i]["title"]);
		}
		$ret->recordsFiltered=count($res);
		$ret->recordsTotal=count($total);
		$this->ajaxReturn($ret);
	}
	private function getRule($id){
		$z=M('auth_group');
		if($id){
			$relust=$z->field("rules")->where("id={$id}")->find();
		}else{
			$relust=$z->field("rules")->where("id=3")->find();
		}
		return $relust['rules'];
	}
	private function getUser($group_id){
		$z=M('auth_group_access');
		if($group_id){
			$result=$z->field("uid")->where("group_id={$group_id}")->select();
		}else{
			$result=$z->field("uid")->where("group_id=3")->select();
		}
		$data=array();
		for($i=0;$i<count($result);$i++){
			$data[]=$result[$i]['uid'];
		}
		$str=implode(',',$data);//将数组转成字符串用','连接
		return $str;
	}
	public function userlist(){
		$group_id=$_POST['id'];
		$uid=$this->getUser($group_id);
		if($uid){
			$columnindex=$_GET['order'][0]['column'];//获取升降序根据的列的索引
			$dir=$_GET['order'][0]['dir'];//根据什么排序
			$start=$_GET['start'];//表格
			$length=$_GET['length'];
			$search=$_GET['search']['value'];
			$m=M('userinfo');
			if($columnindex==1){
				$column='nickname';
			}else if($columnindex==2){
				$column='username';
			}
			$res=$m->field('username,nickname')->where("id in(".$uid.")")->select();
//			$res=$m->where("id in(".$uid.") and nickname like '%{$search}%' or username like '%{$search}%'")->select();
			$total=$m->where("id in(".$uid.")")->select();
			$ret=(object)[];
			//创建一个对象
			$ret->data=array();
			$ret->draw=$_GET['draw'];
			$ret->search=$search;
			for($i=0;$i<count($res);$i++){
				$ret->data[]=array($i+1,$res[$i]["nickname"],$res[$i]["username"]);
			}
			$ret->recordsFiltered=count($res);
			$ret->recordsTotal=count($total);
			$this->ajaxReturn($ret);
		}else{
			$obj->code=1;
			$this->ajaxReturn($obj);
		}
	}
	public function adduserlist(){
		$group_id=$_POST['getid'];
		$z=M('auth_group_access');
		$result=$z->field("uid")->select();
		$data=array();
		for($i=0;$i<count($result);$i++){
			$data[]=$result[$i]['uid'];
		}
		$str=implode(',',$data);//将数组转成字符串用','连接;获得所有uid字符串
		$columnindex=$_GET['order'][0]['column'];//获取升降序根据的列的索引
		$dir=$_GET['order'][0]['dir'];//根据什么排序
		$start=$_GET['start'];//表格
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		$m=M('userinfo');
		if($columnindex==1){
			$column='nickname';
		}else if($columnindex==2){
			$column='username';
		}
		$res=$m->field('username,nickname,id')->where("id not in(".$str.",1)")->select();
//		$this->ajaxReturn($ret);
//		$res=$m->where("username like '%{$search}%' or nickname like '%{$search}%' and id not in(".$str.",1)")->limit("{$start},{$length}")->select();
		$total=$m->select();
		$ret=(object)[];
		//创建一个对象
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($res);$i++){
			$ret->data[]=array($i+1,$res[$i]["nickname"],$res[$i]["username"]);
		}
		$ret->recordsFiltered=count($res);
		$ret->recordsTotal=count($total);
		$this->ajaxReturn($ret);
	}
};
?>